package com.bookmall.cart.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bookmall.cart.domain.vo.OrderPageResVo;
import com.bookmall.cart.domain.vo.OrderResponseVo;
import com.bookmall.cart.domain.vo.PersonParamVo;
import com.bookmall.common.utils.MyQuery;
import com.bookmall.common.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.bookmall.cart.domain.OrderInfo;
import com.bookmall.cart.service.IOrderInfoService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 订单信息Controller
 *
 * @author huyulu
 * @date 2022-04-21
 */
@RestController
@RequestMapping("/order/info")
public class OrderInfoController extends BaseController {
    @Autowired
    private IOrderInfoService orderInfoService;

    /**
     * 根据会员id查询订单信息
     * @author 胡菜鸡
     * @date 2022/4/27 15:47
     */
    @PreAuthorize("@ss.hasPermi('order:info:query')")
    @PostMapping("/orders/{memberId}")
    public AjaxResult getOrders(@PathVariable("memberId") String memberId, @RequestBody PersonParamVo personParamVo)
    {
        OrderPageResVo list = orderInfoService.getOrderByMemberId(memberId, personParamVo);
        return AjaxResult.success(list);
    }

    /**
     * 查询订单信息列表
     */
    @PreAuthorize("@ss.hasPermi('order:info:list')")
    @GetMapping("/list")
    public AjaxResult list(OrderInfo orderInfo)
    {
        IPage<OrderInfo> page = orderInfoService.page(new MyQuery<OrderInfo>().getPage(orderInfo), new QueryWrapper<>(orderInfo));
        PageUtils pageUtils = new PageUtils(page);
        return AjaxResult.success(pageUtils);
    }

    /**
     * 导出订单信息列表
     */
    @PreAuthorize("@ss.hasPermi('order:info:export')")
    @Log(title = "订单信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderInfo orderInfo)
    {
        List<OrderInfo> list = orderInfoService.list(new QueryWrapper<>(orderInfo));
        ExcelUtil<OrderInfo> util = new ExcelUtil<OrderInfo>(OrderInfo.class);
        util.exportExcel(response, list, "订单信息数据");
    }

    /**
     * 获取订单信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('order:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(orderInfoService.getById(id));
    }

    /**
     * 新增订单信息
     */
    @PreAuthorize("@ss.hasPermi('order:info:add')")
    @Log(title = "订单信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrderInfo orderInfo)
    {
        return toAjax(orderInfoService.save(orderInfo));
    }

    /**
     * 修改订单信息
     */
    @PreAuthorize("@ss.hasPermi('order:info:edit')")
    @Log(title = "订单信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderInfo orderInfo)
    {
        return toAjax(orderInfoService.updateById(orderInfo));
    }

    /**
     * 删除订单信息
     */
    @PreAuthorize("@ss.hasPermi('order:info:remove')")
    @Log(title = "订单信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<String> ids)
    {
        return toAjax(orderInfoService.removeBatchByIds(ids));
    }
}
